﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SCM.Model;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SCM.Manage.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            ViewBag.LoginUserName = CurrentUserName;
            ViewBag.LoginUserRoleName = CurrentRoleName;
            ViewBag.LoginUserIcon = "/resources/img/default_headIcon.png";
            ViewBag.Menus = GetMenu();
            return View();
        }

        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <returns></returns>
        private List<LoginMenuVo> GetMenu()
        {
            //根据当前用户角色，获取权限
            var service = GetService<IFunctionService>();
            var functionList = service.GetList(CurrentRoleID);

            //子级权限
            var childs = functionList.Where(p => p.ParentID != null).Select(p => new LoginMenuVo()
            {
                ID = p.ID,
                FunctionName = p.FunctionName,
                FunctionUrl = p.FunctionUrl,
                FunctionIndex = p.FunctionIndex,
                FunctionIcon = p.FunctionIcon,
                ParentID = p.ParentID
            }).GroupBy(p => p.ParentID).ToDictionary(p => p.Key, p => p.OrderBy(p => p.FunctionIndex).ToList());

            //父级权限
            List<LoginMenuVo> menus = functionList.Where(p => p.ParentID == null).OrderBy(p => p.FunctionIndex).Select(p => new LoginMenuVo()
            {
                ID = p.ID,
                FunctionName = p.FunctionName,
                FunctionUrl = p.FunctionUrl,
                FunctionIndex = p.FunctionIndex,
                FunctionIcon = p.FunctionIcon,
                ParentID = p.ParentID,
                ChildMenu = childs.ContainsKey(p.ID) ? childs[p.ID] : null
            }).ToList();

            return menus;
        }


        /// <summary>
        /// 欢迎界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Welcome()
        {
            int days = 29;
            DateTime currTime = DateTime.Now.AddDays(-29);

            Dictionary<string, string> dateList = new Dictionary<string, string>();
            string key = string.Empty;
            int value = 0;
            for (int i = 0; i <= days; i++)
            {
                value = 0;
                key = currTime.ToString("yyyy-MM-dd");
                dateList.Add("'" + currTime.ToString("MM-dd") + "'", "'" + value + "'");
                currTime = currTime.AddDays(1);
            }

            ViewBag.DateListStr = string.Join(",", dateList.Keys);
            ViewBag.SignListStr = string.Join(",", dateList.Values);
            return View();
        }

        /// <summary>
        /// 自定义404页面
        /// </summary>
        /// <param name="statusCode"></param>
        /// <returns></returns>
        [Route("Home/Error/{statusCode}")]
        public IActionResult Error(int statusCode)
        {
            return View();
        }

        /// <summary>
        /// 安全退出
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> OutLogin()
        {
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            return RedirectToAction("Index", "Login");
        }
    }
}
